#!/bin/sh -x
# shell script for automating musictracker project releases
# $1 = 'release' or 'snapshot'
# $2 = version number for release

# clean
rm -r *

# assign a version number
if [ $1 != "release" ]
then
 VERSION=snapshot-`date -u +%Y%m%d%H%M%S`
 WORKDIR=pidgin-musictracker-snapshot
else
 VERSION=$2
 WORKDIR=pidgin-musictracker-$VERSION
fi

# export source from svn
if [ $1 != "release" ]
then
 svn export https://pidgin-musictracker.googlecode.com/svn/trunk $WORKDIR
else
 svn copy https://pidgin-musictracker.googlecode.com/svn/trunk/ https://pidgin-musictracker.googlecode.com/svn/tags/$VERSION -m "Tagging release version $VERSION"
 svn export https://pidgin-musictracker.googlecode.com/svn/tags/$VERSION $WORKDIR
fi

# record version number
echo $VERSION >$WORKDIR/version

# run autotools
(cd $WORKDIR ; ./reconf)

# generate .tar.bz2 file
tar -cjf pidgin-musictracker-$VERSION.tar.bz2 $WORKDIR

# generate .ebuild file
cp $WORKDIR/packaging/pidgin-musictracker.ebuild pidgin-musictracker-$VERSION.ebuild

# build win32
# (this symlink is needed as we depend on pidgin source for some make infrastructure)
ln -s ~/pidgin pidgin
(cd $WORKDIR/src ; make -f Makefile.mingw installer)

# build linux
(cd $WORKDIR/ ; ./configure ; make)
tar -cjf pidgin-musictracker-$VERSION-bin.tar.bz2 -C ./$WORKDIR/src/.libs musictracker.so

# upload files
if [ $1 != "release" ]
then
 # (assume that ~/.netrc provides the credentials for $UPLOAD_HOST)
 lftp -c "open $UPLOAD_HOST; \
         cd pidgin-musictracker-snapshot ; \
         put pidgin-musictracker-$VERSION.ebuild ; \
         put pidgin-musictracker-$VERSION-bin.tar.bz2; \
         put $WORKDIR/installer/pidgin-musictracker-$VERSION.exe ; \
         put pidgin-musictracker-$VERSION.tar.bz2 ; \
         "
else
 #  use googlecode_upload.py to upload the files
 ~/googlecode_upload.py -s "Gentoo Linux ebuild"      -p pidgin-musictracker -l Featured,Beta,OpSys-Linux,Type-Package     pidgin-musictracker-$VERSION.ebuild 
 ~/googlecode_upload.py -s "Linux x86 plugin binary"  -p pidgin-musictracker -l Featured,Beta,OpSys-Linux,Type-Executable  pidgin-musictracker-$VERSION-bin.tar.bz2
 ~/googlecode_upload.py -s "Windows plugin installer" -p pidgin-musictracker -l Featured,Beta,OpSys-Windows,Type-Installer $WORKDIR/installer/pidgin-musictracker-$VERSION.exe
 ~/googlecode_upload.py -s "Source package"           -p pidgin-musictracker -l Featured,Beta,OpSys-All,Type-Source        pidgin-musictracker-$VERSION.tar.bz2
fi

